package com.wondream.myframework.app.request.unipay;

import com.alibaba.druid.support.json.JSONUtils;
import com.wondream.myframework.app.request.BaseRequest;
import com.wondream.myframework.app.common.util.HttpUtil;
import com.wondream.myframework.app.common.util.MD5Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseUnitpayRequest {

    public final String merchantKey = "C45FBF52D47A91C3EAA47B2A0D5D8D57";

    //    public String url = "http://appgatewaystg.52bill.com/online-offline";
//    public String url = "http://miniappgatewaystg.52bill.com/online-offline";
//    public String url = "http://10.201.230.206:8080/online-offline";
//    public String url = "http://localhost:8080/unitpay-online";
//    public String url = "http://10.202.42.21:8080/unitpay-online";
    public String url = "https://upstg.52bill.com/unitpay-online";

    //    public String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzZXNzaW9uS2V5IjpudWxsLCJvcGVuSWQiOiJvUmNBazVTR2xJbWVfNHpCNWhrdXpyUVFzNGs4Iiwic3ViIjoiMTAwMDY0IiwiaWF0IjoxNjIwMjY5Mjg0LCJleHAiOjE2MjAzNTU2ODR9.36S6Y1lM-k8YBqvDY8PxR6qE6Cuql3cudsEboFnNSd7GJ9bjrZnb9TnqSWUbFYvvjrqlUSXVDjv0NaOAgq5s4w";
    public String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzZXNzaW9uS2V5IjpudWxsLCJvcGVuSWQiOiJvUmNBazVTR2xJbWVfNHpCNWhrdXpyUVFzNGs4Iiwic3ViIjoiODE3Q0REMTE1Mzg3MzA3QzcwQ0UwREQ5Mjk2QkIxRDUiLCJpYXQiOjE2MjMyOTAwNTksImV4cCI6MTYyMzM3NjQ1OX0.OwX8jhUJ7uolzkKpk4-oixuTuk_2uje4URmGDKvyGkLawSgUn6zho3VLLqLM-zix3hmyuGvSIbH0m_5SIukqgA";

    public final Log logger = LogFactory.getLog(BaseRequest.class);

    public final Logger logger4j = LoggerFactory.getLogger(BaseRequest.class);

    public Map<String, String> dataMap = new HashMap<String, String>();

    public final String md5key = "BD1FAD3DF7DE2AC6EE9F0C7C8CEFC3F6";
    public final String billMercMd5key = "7ADD09FCB77E64344F3725DDC0A3A9EF";


    public final String busiOnlinePayMd5key="3FBABDD80CC438613E4324A8CA447B66";

    public String apiSignature = "";
    public String response = "";
    public Map responseDataMap = null;

    public abstract void execute();
    public abstract void initSendData();

    public String getApiSignature(){
        apiSignature = MD5Util.apiSign(dataMap, billMercMd5key, logger);
        logger.info("");
        logger.info("==========当前api请求签名： "+apiSignature);
        logger.info("");
        return apiSignature;
    }

    public void addCommonSendData(){
        //////////////////增加默认数据/////////////////
//        dataMap.put("charSet", "02");
//        dataMap.put("signType", "MD5");
//        dataMap.put("version", "1.0");
//        dataMap.put("txDate", "20210510");
//        dataMap.put("txTime", "120236");
        //////////////////增加默认数据/////////////////
    }

    public String sendApiRequest(){
        url = url+"/BBGUnitpay";
        this.addCommonSendData();
        this.initSendData();
        dataMap.put("signature", this.getApiSignature());
        for(int i = 0 ; i < 1 ; i++) {
            if(i==1025){
                System.out.println("1025");
            }
//            response = HttpUtil.httpClientPost(url, dataMap, token);
            response = HttpUtil.sendPost(url, dataMap, token);
            logger.info("");
            logger.info("============================ 请求返回结果：");
            logger.info(response);
            logger.info("");
            if (response != null && response != "") {
                try {
                    responseDataMap = (Map) JSONUtils.parse(this.response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return response;
    }
}